export enum PieceType {
    EMPTY = '.',
    PAWN = 'p',
    ROOK = 'r',
    KNIGHT = 'k',
    BISHOP = 'b',
    ADVISOR = 'a',
    CANNON = 'c',
    GENERAL = 'g'
}

export function opponentColor(color: PieceColor): PieceColor {
    switch (color) {
        case PieceColor.RED: return PieceColor.DARK;
        case PieceColor.DARK: return PieceColor.RED;
        case PieceColor.BLANK: return PieceColor.BLANK;
        default: throw new Error('Invalid color');
    }
}

export function toPieceType(str: string): PieceType {
    switch (str.toLowerCase()) {
        case '.':
            return PieceType.EMPTY;
        case 'p':
            return PieceType.PAWN;
        case 'r':
            return PieceType.ROOK;
        case 'k':
            return PieceType.KNIGHT;
        case 'b':
            return PieceType.BISHOP;
        case 'a':
            return PieceType.ADVISOR;
        case 'c':
            return PieceType.CANNON;
        case 'g':
            return PieceType.GENERAL;
        default:
            throw new Error(str + ' is not a valid piece');
    }
}

export enum PieceColor {
    BLANK = 0,
    DARK = 1,
    RED = 2
}

export class Piece {
    constructor(type: PieceType, color: PieceColor, row: number, col: number) {
        this.type = type;
        this.color = color;
        this.x = row;
        this.y = col;
    }

    public get row() {
        return this.x;
    }

    public get col() {
        return this.y;
    }

    public moveTo(row: number, col: number) {
        this.x = row;
        this.y = col;
    }

    public toString() {
        return this.type[this.color === PieceColor.RED ? 'toUpperCase' : 'toLowerCase']();
    }

    public toPrettyPrint(): string {
        if (this.color === PieceColor.RED) {
            switch (this.type) {
                case PieceType.CANNON: return '砲';
                case PieceType.GENERAL: return '帥';
                case PieceType.ADVISOR: return '仕';
                case PieceType.ROOK: return '俥';
                case PieceType.BISHOP: return '相';
                case PieceType.PAWN: return '兵';
                case PieceType.KNIGHT: return '傌';
                default: return '';
            }
        } else if (this.color === PieceColor.DARK) {
            switch (this.type) {
                case PieceType.CANNON: return '炮';
                case PieceType.GENERAL: return '將';
                case PieceType.ADVISOR: return '士';
                case PieceType.ROOK: return '車';
                case PieceType.BISHOP: return '象';
                case PieceType.PAWN: return '卒';
                case PieceType.KNIGHT: return '馬';
                default: return '';
            }
        } else {
            return '┼';
        }
    }

    public equals(piece: Piece | undefined): boolean {
        if (piece == null) {
            return false;
        }
        return this.x === piece.x && this.y === piece.y && this.type === piece.type && this.color === piece.color;
    }

    public readonly type: PieceType;
    public readonly color: PieceColor;
    private x: number;
    private y: number;
}
